/*
 *  SoundElementTester.cpp
 *  SoundController0002
 *
 *  Created by jiro on 10/11/13.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */

#include "SoundElementTester.h"
#include "../SoundController.h"
#include "CustomAssert.h"

namespace sc {

	void SoundElementTester::TestAll()
	{
		Test_Constuctor_001();

		Test_GetAudioData_001();

		Test_GetAudioData_002();

	}

	void SoundElementTester::Test_Constuctor_001()
	{
		NoteInfo info;

		info.duration = 3.3333;
		info.dynamics = 0.625;

		info.frequency = 92.4986;
		const WaveTable& dynamicsTable1 = WaveTableFactory::Create( WaveTable::Type_Sine, 1.0 / ( 2.0 * info.duration ), info.dynamics );
		const WaveTable& soundTable1 = WaveTableFactory::Create( WaveTable::Type_Sine, info.frequency, 1.0 );
		const SoundElement32 element1 = SoundElement32( soundTable1, dynamicsTable1, info.duration );

		info.frequency = 138.591;
		const WaveTable& dynamicsTable2 = WaveTableFactory::Create( WaveTable::Type_Sine, 1.0 / ( 2.0 * info.duration ), info.dynamics );
		const WaveTable& soundTable2 = WaveTableFactory::Create( WaveTable::Type_Sine, info.frequency, 1.0 );
		const SoundElement32 element2 = SoundElement32( soundTable2, dynamicsTable2, info.duration );

		AssertTrue( element1 != element2 );
		
	}

	void SoundElementTester::Test_GetAudioData_001()
	{
		double time = 0;
		double length = 1.5;
		double powerOfX = 1;
		const WaveFunction& function = WaveFunctionFactory::CreateXSinX( powerOfX, 2 * length );
		const WaveTable& dynamicsTable = WaveTableFactory::Create( function, 1 );
		const WaveTable& soundTable = WaveTableFactory::Create( WaveTable::Type_Constant, 1, 1 );
		const SoundElement64* element = SoundElementFactory64::Create( soundTable, dynamicsTable, length );

		double delta = 1.0 / Sampling_Rate_As_Double;
		for ( ; time < length; time += delta ) {
			AudioData32 data = element->GetAudioData( time );
			AudioData32 functionValue = function( time );
			AssertEqualWithError( data, functionValue, 0.000001 );
			if ( data != functionValue ) {
				cout << "time: " << time << endl;
			}
		}
	}
	
	void SoundElementTester::Test_GetAudioData_002()
	{
		double time = 0;
		double length = 1.5;
		double powerOfX = 1;
		const WaveFunction& function = WaveFunctionFactory::CreateXSinX( powerOfX, 2 * length );
		const WaveFunction& functionRev = WaveFunctionFactory::CreateXSinX( powerOfX, 2 * length, true );

		const WaveTable& soundTable = WaveTableFactory::Create( WaveTable::Type_Constant, 1, 1 );
		const WaveTable& dynamicsTable = WaveTableFactory::Create( function, 1 );
		const WaveTable& dynamicsTableRev = WaveTableFactory::Create( functionRev, 1 );

		const SoundElement64* element = SoundElementFactory64::Create( soundTable, dynamicsTable, length );
		const SoundElement64* elementRev = SoundElementFactory64::Create( soundTable, dynamicsTableRev, length );
		
		double delta = 1.0 / Sampling_Rate_As_Double;
		time = delta;
		for ( ; time < length; time += delta ) {
			stringstream ss;
			//ss << "time: " << time;
			AssertEqualWithError( element->GetAudioData( time ), elementRev->GetAudioData( length - time ), 0.000001 );
//			AssertEqualWithMessage( element->GetAudioData( time ), elementRev->GetAudioData( length - time ), ss.str() );
		}
	}

}
